<template>
  <div class="">
    <el-card>
      <el-table :data="allRoles" border style="width: 100%">
        <el-table-column
          type="index"
          :label="$t('msg.role.index')"
          width="120"
        ></el-table-column>
        <el-table-column
          :label="$t('msg.role.name')"
          prop="title"
        ></el-table-column>
        <el-table-column
          :label="$t('msg.role.desc')"
          prop="describe"
        ></el-table-column>
        <el-table-column
          :label="$t('msg.role.action')"
          width="200"
          #default="{ row }"
        >
          <el-button
            type="primary"
            size="mini"
            @click="onShowPermissionClick(row)"
            v-permission="['distributePermission']"
          >
            {{ $t('msg.role.assignPermissions') }}
          </el-button>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-size="size"
        :total="total"
        :page-sizes="[10, 20]"
        layout="total,sizes,prev,pager,next,jumper"
      ></el-pagination>
    </el-card>
    <permission-dialog
      v-model="permissionDialogVisible"
      :roleId="selectRoleId"
    ></permission-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { roleList } from '@/api/role'
import { watchSwitchLang } from '@/utils/i18n'
import PermissionDialog from './components/DistributePermission.vue'
const allRoles = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(10)
const getRoleList = async () => {
  const result = await roleList({
    page: page.value,
    size: size.value
  })
  allRoles.value = result.items
  total.value = result.total
}
getRoleList()
watchSwitchLang(getRoleList)

const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getRoleList()
}
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getRoleList()
}

const permissionDialogVisible = ref(false)
const selectRoleId = ref('')

const onShowPermissionClick = (row) => {
  permissionDialogVisible.value = true
  selectRoleId.value = row.id
}
</script>

<style lang="scss" scoped>
.pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
